package org.jeecg.modules.demo.crm.mapper;

import java.math.BigDecimal;
import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.jeecg.modules.demo.crm.entity.CInvoiceApply;
import org.jeecg.modules.demo.crm.entity.CInvoiceApplySplit;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.jeecg.modules.demo.crm.entity.CPaymentRecord;

/**
 * @Description: 开票登记拆分发票--收款记录
 * @Author: jeecg-boot
 * @Date:   2025-06-30
 * @Version: V1.0
 */
public interface CInvoiceApplySplitMapper extends BaseMapper<CInvoiceApplySplit> {


    @Select("select  IFNULL(SUM(split_amount), 0)  from c_invoice_apply_split where invoice_apply_id =#{invoiceApplyId} and payment_record_id=#{paymentRecordId}")
    BigDecimal findbyInvoiceAndinvoIceApplyId(@Param("invoiceApplyId") String invoiceApplyId,@Param("paymentRecordId") String paymentRecordId);


    @Select("select  IFNULL(SUM(split_amount), 0)  from c_invoice_apply_split where invoice_apply_id =#{invoiceApplyId} and payment_record_id!=#{paymentRecordId}")
    BigDecimal findbyInvoiceNot(@Param("invoiceApplyId") String invoiceApplyId,@Param("paymentRecordId") String paymentRecordId);

    @Delete("delete from c_invoice_apply_split where payment_record_id =#{paymentRecordId}")
    void deleteSplit(@Param("paymentRecordId") String paymentRecordId);

    @Select("select  IFNULL(SUM(split_amount), 0)  from c_invoice_apply_split where invoice_apply_id =#{invoiceApplyId}")
    BigDecimal findbyInvoice(@Param("invoiceApplyId") String invoiceApplyId);

    @Select(" select  t2.* from c_invoice_apply_split as t1 LEFT JOIN c_payment_record as t2 on t1.payment_record_id = t2.id where  t2.id is not NULL and t1.invoice_apply_id =#{invoiceApplyId}")
    CPaymentRecord getInvoiceTime(@Param("invoiceApplyId") String invoiceApplyId);

}
